package com.mucd.chat.service.support;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.vo.Friend;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * @author mucd
 */
@Service
@Slf4j
public class FriendServiceSupport {
    /**
     * 判断是否验证通过,只算待确认的
     * 查询待确认的好友请求
     *
     * @param friendRelations 好友关系
     */
    public List<Friend> checkRelationStatus(List<Friend> friendRelations) {
        List<Friend> sortedAndDistinct = friendRelations.stream()
                // 保留 relationStatus = Constants.RELATION_STATUS_Pending 的数据
                .filter(friend -> Constants.RELATION_STATUS_PENDING.equals(friend.getRelationStatus()))
                .sorted(Comparator.comparing(Friend::getSendUserId)
                        // 先按 userId1 排序，然后按 timestamp 倒序排序
                        .thenComparing(Friend::getRelationId).reversed())
                .collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Friend::getSendUserId).thenComparing(Friend::getRelationId))),
                                ArrayList::new
                        )
                );

        return sortedAndDistinct;
    }

    public void setRelationStation(List<Friend> friendRelations) {
        friendRelations.forEach(item -> {
            if (Constants.RELATION_STATUS_PENDING.equals(item.getRelationStatus())) {
                item.setRelationStatusName("待确认");
            } else if (Constants.RELATION_STATUS_CONFIRM.equals(item.getRelationStatus())) {
                item.setRelationStatusName("已同意");
            } else if (Constants.RELATION_STATUS_REJECT.equals(item.getRelationStatus())) {
                item.setRelationStatusName("已拒绝");
            }
        });
    }
}
